/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPackage.Views;

import MainPackage.Nahrungsmittel;
import MainPackage.NahrungsmittelList;
import javax.swing.JOptionPane;

/**
 *
 * @author n.kaminski d.chauvistre
 */
public class LebensmittelView extends javax.swing.JDialog {

    private NahrungsmittelList Nahrung = null;
    
    /**
     * Creates new form LebensmittelView
     */
    public LebensmittelView(java.awt.Frame parent, boolean modal, NahrungsmittelList nahrung) {
        super(parent, modal);
        
        this.Nahrung = nahrung;
        
        
        
        initComponents();
        this.lebensmittellistelabel.setText("Lebensmittel Liste ["+this.Nahrung.Count()+"]");
        this.Nahrung.FillJList(NamenListBox);
        Nahrungsmittel.FillJTable_Empty(InhaltTable);
        System.out.println("Hallo ");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        NamenListBox = new javax.swing.JList();
        lebensmittellistelabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        NameLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        InhaltTable = new javax.swing.JTable();
        NeuButton = new javax.swing.JButton();
        BearbeitenButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lebensmittel");
        setResizable(false);

        NamenListBox.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        NamenListBox.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        NamenListBox.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                NamenListBoxValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(NamenListBox);

        lebensmittellistelabel.setText("Lebensmittel Liste");

        jLabel2.setText("Inhaltsstoffe");

        NameLabel.setText("Ausgewählt: ");

        jScrollPane2.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        InhaltTable.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        InhaltTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(InhaltTable);

        NeuButton.setBackground(getBackground());
        NeuButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/Neu.png"))); // NOI18N
        NeuButton.setText("Neues Lebensmittel");
        NeuButton.setBorder(null);
        NeuButton.setContentAreaFilled(false);
        NeuButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        NeuButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NeuButtonActionPerformed(evt);
            }
        });

        BearbeitenButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/Edit.png"))); // NOI18N
        BearbeitenButton.setText("Lebensmittel bearbeiten");
        BearbeitenButton.setBorderPainted(false);
        BearbeitenButton.setContentAreaFilled(false);
        BearbeitenButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        BearbeitenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BearbeitenButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lebensmittellistelabel)
                    .addComponent(NameLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(NeuButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BearbeitenButton)))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 135, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lebensmittellistelabel)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(NameLabel))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NeuButton)
                    .addComponent(BearbeitenButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * Aktualisiert ListBox mit Daten des ausgewählten Nahrungsmittels
     * @param evt 
     */
    private void NamenListBoxValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_NamenListBoxValueChanged
        Nahrungsmittel nm = this.Nahrung.getNahrungsmittelbyIndex(this.NamenListBox.getSelectedIndex());
        NameLabel.setText("Ausgewählt: "+nm.getName());
        nm.FillJTable(InhaltTable);
    }//GEN-LAST:event_NamenListBoxValueChanged

    /**
     * Ruft Fenster zum Nahrungsmittel erstellen auf
     * @param evt 
     */
    private void NeuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NeuButtonActionPerformed
        NahrungsmittelErstellen.showToNew(null, Nahrung);
    }//GEN-LAST:event_NeuButtonActionPerformed
    
    /**
     * Ruft Fenster zum Nahrungsmittel bearbeiten auf
     * @param evt 
     */
    private void BearbeitenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BearbeitenButtonActionPerformed
        if (this.NamenListBox.getSelectedIndex()==-1){
            JOptionPane.showMessageDialog(this,"Nahrungsmittel auswählen!","Error",JOptionPane.ERROR_MESSAGE);
        }
        else{
            NahrungsmittelErstellen.showToOld(null, Nahrung,this.NamenListBox.getSelectedIndex());
        }
    }//GEN-LAST:event_BearbeitenButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BearbeitenButton;
    private javax.swing.JTable InhaltTable;
    private javax.swing.JLabel NameLabel;
    private javax.swing.JList NamenListBox;
    private javax.swing.JButton NeuButton;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lebensmittellistelabel;
    // End of variables declaration//GEN-END:variables
}
